exports.bulk_purchase_detail = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([

        //1.拼团活动
        function (cb) {
            
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_bulk_purchase_activity_info + "/oa_id/" + restfulParams.oa_id,
                    method: 'GET',
                },
                function (err, data) {

                    //console.log(data);

                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.activityData = data.data[0];

                    minCount = resultData.activityData.bpac_min_count;

                    cb();
                }
            );

        },
        //拼团详情
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityContent + "/bpac_oaid/" + restfulParams.oa_id,
                    method: 'GET',
                },
                function (err, data) {

                    //console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.activityContent = data.data;

                    cb();
                }
            );

        },

        //获取机构信息
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + resultData.activityData.oa_oid,
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationData = data.data[0];

                    cb();
                }
            );

        },

        //2.团长
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_id/" + restfulParams.bpau_id,
                    method: 'POST',
                    data: {
                        fields: ["bpau_id","bpau_uid", "bpau_user_count", "u_nickname","bpau_fid", "u_header_url","bpau_member_count"].join(",")
                    }
                },
                function (err, data) {

                    //console.log(data);

                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    

                    resultData.joinUserData = data.data;

                    //console.log("-----------");
                    //console.log(resultData.joinUserData)

                    var differ = resultData.joinUserData[0].bpau_member_count - resultData.joinUserData[0].bpau_user_count;
                    
                    if(differ <= 0){
                        resultData.isComplete = true;
                    }else{
                        resultData.isComplete         = false;
                        resultData.differUserComplete = differ;
                    }

                    //判断当前用户是不是团长
                    if( restfulParams.u_id == resultData.joinUserData[0].bpau_uid ){

                        resultData.isMyGroup = true;

                    }else{
                        
                        resultData.isMyGroup = false;
                    }
                    
                    cb();
                }
            );

        },

        //判断是否参与过别的团
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id + "/bpau_uid/" + restfulParams.u_id + "/bpau_is_complete/0",
                    method: 'POST',
                    data: {
                        fields: ["bpau_id","bpau_oaid","bpau_member_count"].join(",")
                    }
                },
                function (err, data) {
                    //console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    if(data.data.length > 0){
                        resultData.isParticipationOtherGroup = true;
                        resultData.isParticipationOtherOaId  = data.data[0].bpau_oaid;
                        resultData.isParticipationOtherId  = data.data[0].bpau_id;
                        resultData.completeMemberCount  = data.data[0].bpau_member_count;
                    }else{
                        resultData.isParticipationOtherGroup = false;
                    }

                    cb();
                }
            );


        },

        //其他团员
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id + "/bpau_fid/" + restfulParams.bpau_id,
                    method: 'POST',
                    data: {
                        fields: ["bpau_id","bpau_uid", "bpau_user_count", "u_nickname","bpau_fid", "u_header_url"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.joinUserData = resultData.joinUserData.concat(data.data);

                    resultData.isParticipation = false;

                    //判断我是否参与了这个团
                    for(var i=0;i<resultData.joinUserData.length;i++){

                        if(restfulParams.u_id == resultData.joinUserData[i].bpau_uid){
                            resultData.isParticipation = true;
                        }

                    }

                    cb();
                }
            );

        },

        //2.拼团活动评论
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.activity_comment_info + "/oac_oaid/"+restfulParams.oa_id+"/skip/0/limit/" + config.publicConfig.LIMIT+"/count/1",
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    resultData.commentCount = data.count;
                    resultData.commentData  = data.data;
                    cb();
                }
            );

        },

        //1.参与人数
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id + "/skip/0/limit/10/count/1",
                    method: 'POST',
                    data: {
                        fields: ["u_nickname", "u_header_url"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.completeBulkPurchaseCount = data.count;
                    resultData.completeBulkPurchaseData = data.data;
                    cb();
                }
            );

        }


        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

};